﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Paltop_.Controllers
{
    public class ZipController
    {
        public static Boolean UnZipFile(string sourcePath, string destPath)
        {
            ComponentAce.Compression.ZipForge.ZipForge archiver = new ComponentAce.Compression.ZipForge.ZipForge();
            try
            {
                string tempDestPath = destPath.Replace("\\", "//");
                tempDestPath = tempDestPath.Substring(0, tempDestPath.LastIndexOf("//"));
                // The name of the ZIP file to unzip
                archiver.FileName = sourcePath;
                // Open an existing archive
                archiver.OpenArchive(System.IO.FileMode.Open);
                // Default path for all operations                
                archiver.BaseDir = tempDestPath;
                // Extract all files from the archive to C:\Temp folder
                archiver.ExtractFiles("*.*");
                // Close archive
                archiver.CloseArchive();
                return true;
            }

            // Catch all exceptions of the ArchiverException type
            catch (ComponentAce.Compression.Archiver.ArchiverException)
            {
                // MessageBox.Show("an error occure");
                return false;
            }
        }

        public static Boolean ZipFolder(string sourcePath, string destPath)
        {
            ComponentAce.Compression.ZipForge.ZipForge archiver = new ComponentAce.Compression.ZipForge.ZipForge();

            try
            {

                // Set the name of the archive file we want to create
                archiver.FileName = destPath;
                // Because we create a new archive, 
                // we set fileMode to System.IO.FileMode.Create
                archiver.OpenArchive(System.IO.FileMode.Create);
                // Set base (default) directory for all archive operations
                string tempSourcePath = sourcePath.Replace("\\", "//");
                tempSourcePath = tempSourcePath.Substring(0, tempSourcePath.LastIndexOf("//"));
                archiver.BaseDir = tempSourcePath;
                // Add the c:\Test folder to the archive with all subfolders
                archiver.AddFiles(sourcePath);
                //FileStream  t=new FileStream(@"C:\Users\SONY\Desktop\t",FileMode.Open);
                //archiver.AddFromStream("test", t);
                archiver.CloseArchive();
                return true;
            }
            // Catch all exceptions of the ArchiverException type
            catch (ComponentAce.Compression.Archiver.ArchiverException)
            {
                // MessageBox.Show("error");
                return false;
            }
        }
    }
}